package com.woniuxy.enjoylife.advice;

import com.woniuxy.enjoylife.utils.RedisKeyGenerator;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


//使用aspectJ代理,在查询service之前自动向redis中存入数据
@Component
@Aspect
public class RedisAdvice {
    @Resource
    private RedisTemplate redisTemplate;

    //切点，暂时设定为所有service
    @Pointcut("execution(* com.woniuxy.enjoylife.service.impl.*.*(..))")
    public void pc(){

    }

    //环绕通知，将数据缓存到redis，redis中无数据再进行查询
    @Around("pc()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object proceed=null;
        try {
            Object target = joinPoint.getTarget();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            //获取参数名的数组
            String[] parameterNames = signature.getParameterNames();
            //获取原方法传入的参数值
            Object[] args = joinPoint.getArgs();

            String key = RedisKeyGenerator.generate(target, signature.getName(),args);
//            redisTemplate.opsForHash().get(target.getClass().getName().toString(),key);

            String typeToString = signature.getName().toLowerCase();
            String cacheType ="user";
            String targetToString = target.toString().toLowerCase();
            if (targetToString.contains("comment")) {
                cacheType = "comment";
            } else if (targetToString.contains("order")) {
                cacheType = "order";
            } else if (targetToString.contains("goods")) {
                cacheType = "goods";
            } else if (targetToString.contains("activity")) {
                cacheType = "activity";
            } else if (targetToString.contains("house") || targetToString.contains("community")) {
                cacheType = "house";
            } else if (targetToString.contains("recent")) {
                cacheType = "recent";
            } else if (targetToString.contains("server")) {
                cacheType = "recent";
            } else if (targetToString.contains("suggest")) {
                cacheType = "suggest";
            } else{
                cacheType = "user";
            }



            if (typeToString.contains("select") || typeToString.contains("find") || typeToString.contains("get") || typeToString.contains("page") || typeToString.contains("all")) {
                if (redisTemplate.opsForHash().hasKey(cacheType,key)){
                    return  redisTemplate.opsForHash().get(cacheType,key);
                }else{
                    proceed = joinPoint.proceed();
                    redisTemplate.opsForHash().put(cacheType,key,proceed);
                }
            }

            redisTemplate.opsForHash().delete(cacheType);
//            proceed = joinPoint.proceed();
//            redisTemplate.opsForHash().put(target.getClass().toString(),signature.getName(),proceed);
//            redisTemplate.opsForValue().set(key,proceed);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }

}